package com.momochen.service;

import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

/**
 * @author 徐飞
 * @version 2016/02/24 19:43
 */
public class NettyServerBootstrap {
    private int port;

    public NettyServerBootstrap(int port) {
        this.port = port;
        bind();
    }

    private void bind() {
        EventLoopGroup boss = new NioEventLoopGroup(1);
        EventLoopGroup worker = new NioEventLoopGroup();
//        ServerBootstrap bootstrap=new ServerBootstrap();
        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(boss, worker);
            bootstrap.channel(NioServerSocketChannel.class);
            bootstrap.option(ChannelOption.SO_BACKLOG, 100);
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel socketChannel) throws Exception {
                    ChannelPipeline p = socketChannel.pipeline();
                    p.addLast(new IdleStateHandler(10 * 60, 10 * 60, 10 * 60, TimeUnit.SECONDS));
////                p.addLast(new ObjectEncoder());
////                p.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
                    p.addLast(new ByteArrayDecoder());
                    p.addLast(new ByteArrayEncoder());
                    p.addLast(new NettyServerHandler());
                }
            });

//            ChannelFuture f = bootstrap.bind(port).sync();
            ChannelFuture f = bootstrap.bind(port).sync();
            if (f.isSuccess()) {
                System.out.println("connect server  成功---------");
            } else {
                System.out.println("connect server  失败---------");
            }
            f.channel().closeFuture().sync();
        } catch (Exception e) {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }

//        bootstrap.group(boss,worker);
//        bootstrap.channel(NioServerSocketChannel.class);
//        bootstrap.option(ChannelOption.SO_BACKLOG, 128);
//        //通过NoDelay禁用Nagle,使消息立即发出去，不用等待到一定的数据量才发出去
//        bootstrap.option(ChannelOption.TCP_NODELAY, true);
//        //保持长连接状态
//        bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
//        bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
//            @Override
//            protected void initChannel(SocketChannel socketChannel) throws Exception {
//                ChannelPipeline p = socketChannel.pipeline();
////                p.addLast(new ObjectEncoder());
////                p.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
//                p.addLast(new StringEncoder());
//                p.addLast(new StringDecoder());
//                p.addLast(new NettyServerHandler());
//            }
//        });
//        ChannelFuture f= bootstrap.bind(port).sync();
//        if(f.isSuccess()){
//            System.out.println("netty server start---------------");
//        }
    }

}
